package com.supreme.commonmongo.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class MongoSaveAOP {

    @Pointcut(value = "execution(* org.springframework.data.mongodb.repository.MongoRepository.save(..))")
    public void PointCut() {
    }

    @Around("PointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        /*Object[] args = joinPoint.getArgs();
        if (args.length > 0) {
            Object saveArg = args[0];
            if (saveArg instanceof MngBaseEntity) {
                MngBaseEntity baseEntity = (MngBaseEntity) saveArg;
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                String nowDatetime = LocalDateTime.now().format(dateTimeFormatter);
                if (Objects.isNull(baseEntity.getId()) || Objects.isNull(baseEntity.getCreateTime())) {
                    baseEntity.setCreateTime(nowDatetime);
                }

                baseEntity.setUpdateTime(nowDatetime);
            }
        }*/

        return joinPoint.proceed();
    }
}
